package com.baosight.risk.service.riri.impl;


import com.baosight.risk.api.riri.IRiriDutyService;
import com.baosight.risk.base.entity.MultipleUpdateStatus;
import com.baosight.risk.base.mapper.BaseMapper;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.ShiroUtils;
import com.baosight.risk.mapper.riri.RiriDutyUserMapper;
import com.baosight.risk.service.riri.entity.RiriDutyUser;
import com.baosight.risk.service.riri.vo.RiriUserVO;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author houji
 */
@Service
public class RiriDutyServiceImpl extends BaseServiceImpl<RiriDutyUser> implements IRiriDutyService {
    @Autowired
    private RiriDutyUserMapper ririDutyUserMapper;

    @Override
    public BaseMapper getMapper() {
        return ririDutyUserMapper;
    }

    @Override
    public List<RiriDutyUser> selectList(RiriDutyUser ririDutyUser) {
        return ririDutyUserMapper.selectList(ririDutyUser);
    }

    @Override
    public Boolean deleteByIds(MultipleUpdateStatus updateStatus) {
        List<String> ids = updateStatus.getIds();
        ririDutyUserMapper.deleteByIds(ids);
        return true;
    }

    @Override
    public void addDutyUser(List<RiriDutyUser> ririDutyUserList) {
        for (RiriDutyUser rdu : ririDutyUserList) {
            rdu.setBizGuid("RISK.AUDITGROUP");
            rdu.setBizType("RISK.AUDITGROUP");
            boolean b = trimUser(rdu);
            if (b) {
                continue;
            }
            rdu = (RiriDutyUser) BeanUtils.initBean(rdu);
            ririDutyUserMapper.insert(rdu);
        }
    }

    @Override
    public void chooseDutyUser(RiriUserVO vo) {
        //更新审核小组成员,先删除后增加
        String importId = vo.getImportId();
        ririDutyUserMapper.deleteByBizGuid(importId);
        List<String> ids = vo.getIds();
        List<RiriDutyUser> userList = ririDutyUserMapper.selectListByIds(ids);
        for (RiriDutyUser rdu : userList
        ) {
            rdu = (RiriDutyUser) BeanUtils.initBean(rdu);
            rdu.setBizGuid(importId);
            rdu.setExt1(importId);
            rdu.setBizType("RISK_FINISH.GROUP_USER");
            ririDutyUserMapper.insert(rdu);
        }
    }

    @Override
    public void addDutyUserByRifi(List<RiriDutyUser> ririDutyUserList) {
        RiriDutyUser user1 = ririDutyUserList.get(0);
        //先删除再添加
       ririDutyUserMapper.deleteByGuidAndType(user1.getBizGuid(), user1.getBizType());
            for (RiriDutyUser user : ririDutyUserList
            ) {
                user = (RiriDutyUser) BeanUtils.initBean(user);
                ririDutyUserMapper.insert(user);
            }
    }

    @Override
    public void updateAudit(RiriDutyUser ririDutyUser) {
        XtzyUser user1 = ShiroUtils.getUser();
        RiriDutyUser userQ = new RiriDutyUser();
        userQ.setBizGuid(ririDutyUser.getBizGuid());
        userQ.setUserCode(user1.getUserCode());
        RiriDutyUser user = ririDutyUserMapper.selectOne(userQ);
        user.setExt1(ririDutyUser.getExt1());
        user.setExt2(ririDutyUser.getExt2());
        user = (RiriDutyUser)BeanUtils.updateBean(user);
        ririDutyUserMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public List<RiriDutyUser> selectListByRifi(RiriDutyUser ririDutyUser) {
        return ririDutyUserMapper.selectListByRifi(ririDutyUser);
    }

    /**
     * 审核小组去重
     */
    private boolean trimUser(RiriDutyUser user) {
        List<RiriDutyUser> select = ririDutyUserMapper.select(user);
        if (CollectionUtils.isNotEmpty(select)) {
            return true;
        }
        return false;
    }

}
